/*
* $Id: SettingsWin.cpp 2 2009-12-23 13:58:02Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Settings/InterfaceStn.h"
#include "../UserMessage.h"
#include "SettingsWin.h"
#include "TreeSettings.h"
#include "ContentSettings.h"

CSettingsWin::CSettingsWin(){
	m_pTreeSettings = NULL;
	m_pContentSettings = NULL;
}
CSettingsWin::~CSettingsWin(){
	if(IsWindow(m_hWnd))
		DestroyWindow();
}
void CSettingsWin::PostNcDestroy(){
	if(m_pTreeSettings)
		delete m_pTreeSettings;
	if(m_pContentSettings)
		delete m_pContentSettings;
}
void CSettingsWin::ApplyButtonState(){
	std::vector<ISetting*> aSettings = IApplication::GetApp()->GetSettingsManager()->GetSettings();
	for(std::vector<ISetting*>::iterator it = aSettings.begin(); it != aSettings.end(); it++){
		if((*it)->IsChanged()){
			m_buttonApply.EnableWindow(true);
			return;
		}
	}
	m_buttonApply.EnableWindow(false);
}
bool CSettingsWin::IsValidate(){
	return m_pContentSettings->IsValidate();
}

BEGIN_MESSAGE_MAP(CSettingsWin, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_CLOSE()
	ON_WM_KEYDOWN()
	ON_COMMAND(CANCEL_BUTTON_ID, OnCancel)
	ON_COMMAND(APPLY_BUTTON_ID, OnApply)
	ON_COMMAND(DEFAULT_BUTTON_ID, OnDefault)
	ON_CONTROL(LBN_SELCHANGE, TREE_SETTINGS_ID, OnSelChange)
	ON_MESSAGE(UM_TABSTOP, OnTabStop)
END_MESSAGE_MAP()

int CSettingsWin::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int nRes = __super::OnCreate(lpCreateStruct);
	m_pTreeSettings = new CTreeSettings(this);
	m_pTreeSettings->Create(WS_CHILD | WS_VISIBLE | LBS_OWNERDRAWFIXED | WS_HSCROLL | WS_BORDER | LBS_NOTIFY,
							CRect(CPoint(5, 5), CSize(250, lpCreateStruct->cy - 70)), this, TREE_SETTINGS_ID);
	m_pContentSettings = new CContentSettings(this);
	m_pContentSettings->Create(NULL, NULL, WS_CHILD | WS_VISIBLE,
							CRect(CPoint(255, 5), CSize(lpCreateStruct->cx - 265, lpCreateStruct->cy - 70)),
							this, CONTENT_SETTINGS_ID);
	m_buttonApply.Create(IApplication::GetApp()->GetLanguageManager()->GetString(APPLY_ID).c_str(),
						WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
						CRect(CPoint(470, lpCreateStruct->cy - 70), CSize(100, 30)), this, APPLY_BUTTON_ID);
	m_buttonCancel.Create(IApplication::GetApp()->GetLanguageManager()->GetString(CLOSED_ID).c_str(),
						WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
						CRect(CPoint(580, lpCreateStruct->cy - 70), CSize(100, 30)), this, CANCEL_BUTTON_ID);
	m_buttonDefault.Create(IApplication::GetApp()->GetLanguageManager()->GetString(DEFAULT_ID).c_str(),
						WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
						CRect(CPoint(5, lpCreateStruct->cy - 70), CSize(130, 30)), this, DEFAULT_BUTTON_ID);
	ApplyButtonState();
	return nRes;
}
void CSettingsWin::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
	dc.FillRect(rect, &brush);
}
void CSettingsWin::OnSelChange(){
	int nSel = m_pTreeSettings->GetCurSel();
	if(nSel != -1){
		Item item = *((Item*)m_pTreeSettings->GetItemData(nSel));
		m_pContentSettings->CreateDinamicInterface(item.m_strValueItem);
	}
}
void CSettingsWin::OnCancel(){
	std::vector<ISetting*> aSettings = IApplication::GetApp()->GetSettingsManager()->GetSettings();
	for(std::vector<ISetting*>::iterator it = aSettings.begin(); it != aSettings.end(); it++)
		(*it)->CancelChange();
	DestroyWindow();
}
void CSettingsWin::OnClose(){
	OnCancel();
}
void CSettingsWin::OnApply(){
	std::vector<ISetting*> aSettings = IApplication::GetApp()->GetSettingsManager()->GetSettings();
	for(std::vector<ISetting*>::iterator it = aSettings.begin(); it != aSettings.end(); it++)
		(*it)->SaveSetting();
	ApplyButtonState();
	m_pContentSettings->UpdateStates();
	IApplication::GetApp()->GetVisualManager()->UpdateVisualComponents();
}

void CSettingsWin::OnDefault(){
	if(MessageBox(IApplication::GetApp()->GetLanguageManager()->GetString(DEFAULT_QUESTION_ID).c_str(),
		(IApplication::GetApp()->GetLanguageManager()->GetString(MAIN_FRAME_CAPTION_ID) + _T(": ")\
		+ IApplication::GetApp()->GetLanguageManager()->GetString(SETTINGS_CAPTION_ID)).c_str(),
		MB_OKCANCEL | MB_ICONQUESTION) == IDOK){
		std::vector<ISetting*> aSettings = IApplication::GetApp()->GetSettingsManager()->GetSettings();
		for(std::vector<ISetting*>::iterator it = aSettings.begin(); it != aSettings.end(); it++)
			(*it)->DefaultSetting();
		OnSelChange();
	}
	ApplyButtonState();
}
LRESULT CSettingsWin::OnTabStop(WPARAM wParam, LPARAM lParam){
	if((void*)lParam == m_pTreeSettings)
		m_pContentSettings->SendMessage(UM_TABSTOP, 0, (LPARAM)this);
	else if((void*)lParam == m_pContentSettings && m_buttonApply.IsWindowEnabled())
		m_buttonApply.SetFocus();
	else if((void*)lParam == m_pContentSettings && !m_buttonApply.IsWindowEnabled() ||
			dynamic_cast<CCommonButton*>((CCommonControl*)lParam) == &m_buttonApply)
		m_buttonCancel.SetFocus();
	else if(dynamic_cast<CCommonButton*>((CCommonControl*)lParam) == &m_buttonCancel)
		m_buttonDefault.SetFocus();
	else
		m_pTreeSettings->SetFocus();
	return TRUE;
}
void CSettingsWin::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){
	if(nChar == VK_TAB)
		OnTabStop(0, 0);
	__super::OnKeyDown(nChar, nRepCnt, nFlags);
}